package com.ymatou.iapi.exp.testcase;

import java.text.DecimalFormat;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.exp.parameter.GetUserExpBean;
import com.ymatou.iapi.exp.service.GetUserExpCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.LevelConfig;
import com.ymttest.database.sqlwapper.ExpWapper;
/**
*获取用户经验值
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserExp{
	private static GetUserExpBean getuserexpBean;
	private static GetUserExpCall getuserexpCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户经验值");
	}

	@Before
	public void caseUp() {
		getuserexpBean=new GetUserExpBean();
		getuserexpCall=new GetUserExpCall();
		ExpCallService.resetFestivalRateConfig(1.0,false);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserexpCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUserExp_001() {
		Logger.start(true,"获取用户经验值成功");
		try {
			ExpCallService.resetExp(1, 20);
			getuserexpBean.setUserId(1);
			getuserexpCall.setData(getuserexpBean);
			getuserexpCall.callService();			
			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
			Logger.verifyEquals(20, rexp, "验证返回报文exp");
			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
			Logger.verifyEquals(1, lv, "验证返回报文lv");
			
			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
			LevelConfig lvinfo = null;
			if(lv<6){
				lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
				Logger.verifyEquals(lvinfo.getExp()-20, distance, "验证返回报文distance");
				
				Logger.verifyEquals(20d/Double.valueOf(lvinfo.getExp()), rate, "验证返回报文distance");
			}else{
				Logger.verifyEquals(0, distance, "验证返回报文distance");
				Logger.verifyEquals(0, rate, "验证返回报文distance");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetUserExp_002() {
		Logger.start(true,"不输入uid，失败");
		try {
//			ExpCallService.resetExp(1, 20);
//			getuserexpBean.setUserId(1);
			getuserexpCall.setData(getuserexpBean);
			getuserexpCall.callService();			
			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
			Logger.verifyEquals(0, rexp, "验证返回报文exp");
			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
			Logger.verifyEquals(1, lv, "验证返回报文lv");
			
			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
			LevelConfig lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
			Logger.verifyEquals(lvinfo.getExp(), distance, "验证返回报文distance");
			
			Logger.verifyEquals(0.0, rate, "验证返回报文distance");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserExp_003() {
		Logger.start(true,"增加经验值后获取用户经验值成功,数据正确");
		try {
			int exp=50;
			ExpCallService.resetExp(1, exp);
			getuserexpBean.setUserId(1);
			getuserexpCall.setData(getuserexpBean);
			getuserexpCall.callService();			
			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
			Logger.verifyEquals(exp, rexp, "验证返回报文exp");
			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
			Logger.verifyEquals(1, lv, "验证返回报文lv");
			
			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
			LevelConfig lvinfo = null;
			if(lv<6){
				lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
				Logger.verifyEquals(lvinfo.getExp()-exp, distance, "验证返回报文distance");
				
				Logger.verifyEquals(exp/Double.valueOf(lvinfo.getExp()), rate, "验证返回报文distance");
			}else{
				Logger.verifyEquals(0, distance, "验证返回报文distance");
				Logger.verifyEquals(0, rate, "验证返回报文distance");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserExp_004() {
		Logger.start(true,"升级后获取用户经验值成功,数据正确");
		try {
			//升级后查询总线消息
//			 String appId = "userservice";
//	         String code = "expchange";
			int exp=3500;
			ExpCallService.resetExp(1, exp);
			getuserexpBean.setUserId(1);
			getuserexpCall.setData(getuserexpBean);
			getuserexpCall.callService();			
			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
			Logger.verifyEquals(exp, rexp, "验证返回报文exp");
			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
			Logger.verifyEquals(2, lv, "验证返回报文lv");
			
			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
			LevelConfig lvinfo = null;
			if(lv<6){
				//经验值百分比从当前等级开始计算
				LevelConfig olvinfo = new ExpWapper().selectlevelconfig(lv).get(0);
				lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
				Logger.verifyEquals(lvinfo.getExp()-exp, distance, "验证返回报文distance");
				int nowExp = lvinfo.getExp()-olvinfo.getExp();
				exp=exp-olvinfo.getExp();
				//设置格式
				DecimalFormat dcmFmt = new DecimalFormat("0.00");
				Logger.verifyEquals(dcmFmt.format(exp/Double.valueOf(nowExp)), dcmFmt.format(rate), "验证返回报文distance");
			}else{
				Logger.verifyEquals(0, distance, "验证返回报文distance");
				Logger.verifyEquals(0, rate, "验证返回报文distance");
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetUserExp_005() {
		Logger.start(true,"最高等级用户获取经验值成功,数据正确");
		try {
			int exp=999999;
			ExpCallService.resetExp(2, exp);
			getuserexpBean.setUserId(2);
			getuserexpCall.setData(getuserexpBean);
			getuserexpCall.callService();			
			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
			Logger.verifyEquals(exp, rexp, "验证返回报文exp");
			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
			Logger.verifyEquals(6, lv, "验证返回报文lv");
			
			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
			LevelConfig lvinfo = null;
			if(lv<6){
				//经验值百分比从当前等级开始计算
				LevelConfig olvinfo = new ExpWapper().selectlevelconfig(lv).get(0);
				lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
				Logger.verifyEquals(lvinfo.getExp()-exp, distance, "验证返回报文distance");
				int nowExp = lvinfo.getExp()-olvinfo.getExp();
				exp=exp-olvinfo.getExp();
				//设置格式
				DecimalFormat dcmFmt = new DecimalFormat("0.00");
				Logger.verifyEquals(dcmFmt.format(exp/Double.valueOf(nowExp)), dcmFmt.format(rate), "验证返回报文distance");
			}else{
				Logger.verifyEquals(0, distance, "验证返回报文distance");
				Logger.verifyEquals(1d, rate, "验证返回报文distance");
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//这个需求被砍了
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_GetUserExp_006() {
//		Logger.start(true,"获取当月取得经验值成功20180425");
//		try {
//			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
//			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
//			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
//			String token=registerResult.get("Token").getAsString();
//			int userId  = urrc.getUserid();
//			ExpCallService.resetExp(userId, 20);
//			getuserexpBean.setUserId(userId);
//			getuserexpCall.setData(getuserexpBean);
//			getuserexpCall.callService();			
//			Logger.verifyEquals("success", getuserexpCall.getErrorMessage(), "验证errormessage");
//			Logger.verifyEquals("true", getuserexpCall.getSuccess(), "验证success");
//			Logger.verifyEquals("0", getuserexpCall.getErrorCode(), "验证ErrorCode");
//			int rexp=getuserexpCall.getJsonObject().getAsJsonObject("result").get("exp").getAsInt();
//			Logger.verifyEquals(20, rexp, "验证返回报文exp");
//			int lv=getuserexpCall.getJsonObject().getAsJsonObject("result").get("level").getAsInt();
//			Logger.verifyEquals(1, lv, "验证返回报文lv");
//			
//			int distance=getuserexpCall.getJsonObject().getAsJsonObject("result").get("distance").getAsInt();
//			double rate=getuserexpCall.getJsonObject().getAsJsonObject("result").get("rate").getAsDouble();
//			LevelConfig lvinfo = null;
//			if(lv<6){
//				lvinfo = new ExpWapper().selectlevelconfig(lv+1).get(0);
//				Logger.verifyEquals(lvinfo.getExp()-20, distance, "验证返回报文distance");
//				
//				Logger.verifyEquals(20d/Double.valueOf(lvinfo.getExp()), rate, "验证返回报文distance");
//			}else{
//				Logger.verifyEquals(0, distance, "验证返回报文distance");
//				Logger.verifyEquals(0, rate, "验证返回报文distance");
//			}
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}